package cn.iocoder.yudao.module.mzt.utill;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageOverlay {

    /**
     * 将覆盖图放置到背景图的指定位置，并返回合成后的图像。
     *
     * @param backgroundFile 背景图的 MultipartFile 对象
     * @param overlayFile    要放置的覆盖图的 MultipartFile 对象
     * @param targetWidth    目标宽度，默认为 266
     * @param targetHeight   目标高度，默认为 176
     * @return 合成后的图像，作为 MultipartFile 对象
     * @throws IOException 如果读取文件或写入文件时发生错误
     */
    public static MultipartFile overlayImages(MultipartFile backgroundFile, MultipartFile overlayFile,
                                              int targetWidth, int targetHeight) throws IOException {
        // 加载背景图
        BufferedImage background = ImageIO.read(backgroundFile.getInputStream());
        System.out.println("背景图加载成功，尺寸: " + background.getWidth() + "x" + background.getHeight());

        // 加载要放置的图片
        BufferedImage overlay = ImageIO.read(overlayFile.getInputStream());
        System.out.println("覆盖图加载成功，尺寸: " + overlay.getWidth() + "x" + overlay.getHeight());

        // 等比例压缩 overlay 图片
        overlay = resizeImage(overlay, targetWidth, targetHeight);
        System.out.println("覆盖图压缩成功，新的尺寸: " + overlay.getWidth() + "x" + overlay.getHeight());

        // 创建一个新的图像，大小与背景图相同
        BufferedImage combined = new BufferedImage(background.getWidth(), background.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = combined.getGraphics();

        // 绘制背景图
        g.drawImage(background, 0, 0, null);

        // 计算镂空位置（假设镂空区域的坐标和大小已知）
        int x = 68; // 镂空区域的 x 坐标
        int y = 40;  // 镂空区域的 y 坐标

        // 将 overlay 图片绘制到镂空位置
        g.drawImage(overlay, x, y, null);
        System.out.println("覆盖图绘制到镂空位置: (" + x + ", " + y + ")");

        // 释放图形上下文
        g.dispose();

        // 将合成图像转换为 MultipartFile
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(combined, "png", baos);
        byte[] imageBytes = baos.toByteArray();
        return new InMemoryMultipartFile(imageBytes, "ITMzt.png");
    }

    // 等比例压缩 overlay 图片的方法
    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        // 创建新的图像
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        g.dispose();

        return resizedImage;
    }

    // InMemoryMultipartFile 类实现
    public static class InMemoryMultipartFile implements MultipartFile {
        private final byte[] content;
        private final String name;

        public InMemoryMultipartFile(byte[] content, String name) {
            this.content = content;
            this.name = name;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public String getOriginalFilename() {
            return name;
        }

        @Override
        public String getContentType() {
            return "image/png"; // 根据需要返回合适的 MIME 类型
        }

        @Override
        public boolean isEmpty() {
            return content.length == 0;
        }

        @Override
        public long getSize() {
            return content.length;
        }

        @Override
        public byte[] getBytes() {
            return content;
        }

        @Override
        public InputStream getInputStream() {
            return new ByteArrayInputStream(content);
        }

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException {
            // 实现文件传输逻辑（可选）
        }
    }
}
